package com.wzh.core.api.balance;

import com.wzh.core.api.LoadBalance;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Random;

/**
 * @description: 随机负载均衡
 * @author: wangzouhuax
 */
public class RandomLoadBalance<T> implements LoadBalance<T> {

    private static final Random random = new Random(System.currentTimeMillis());

    @Override
    public T choose(List<T> urls) {
        if (CollectionUtils.isEmpty(urls)) {
            return null;
        }
        if (urls.size() == 1) {
            return urls.get(0);
        }
        return urls.get(random.nextInt(urls.size()));
    }
}
